import request from "../request"
import qs from "qs"

/**
 *
 *  在文档里面这么几个单词是可以告诉我们指定 参数的位置
 *
 *    path -->  把参数放友url的里面  /admin/update/{id}
 *    query  --->  把参数放在url的search部分   /id/update?id=xxx&name=xxx
 *    body   --->  post请求把数据放在 请求体里面
 *
 */

// 登录以后返回token
export const loginApi = (params: IRparamsData): RSP<IRloginpamse> => request.post('/admin/login', params)
// 获取当前登录用户信息
export const userDataApi = (): RSP<IUserInfoResponse> => request.get("/admin/info")

// 根据用户名或姓名分页获取用户列表
export const getUserList = (params?: IUserListParams): RSP<IUserDate<IUserList>> => request.get("/admin/list", { params })

// 修改帐号状态
export const updateUserStatusApi = (params: IUpdateUserStatusParams): RSP<any> => request.post(`/admin/updateStatus/${params.id}?status=${params.status}`)

// 用户注册
export const registerUseeApi = (params: IRegisterUserParams): RSP<IUserList> => request.post('/admin/register', params)

// 获取指定用户信息
export const getUserInfoApi = (id: number): RSP<IUserList> => request.get(`/admin/${id}`)

// 修改指定用户信息
export const upDateUserInfoApi = (params: IUserList): RSP<any> => request.post(`/admin/update/${params.id}`, params)
// 获取所有角色
export const getRoleListApi = (): RSP<IRoleModel[]> => request.get("/role/listAll");
// 获取指定用户的角色
export const getRoleListByAdminIdApi = (adminId: number): RSP<IRoleModel[]> => request.get(`/admin/role/${adminId}`);


// 给用户分配角色
export const updateRolesApi = ({ adminId, roleIds }: IUpdateRolesParams) => {
    // qs 在转换 数组的时候，有点和我们想要的不一样，所以这个东西，我们还是只能自己转换
    // console.log(qs.stringify(params));
    return request.post(
        `/admin/role/update?adminId=${adminId}&roleIds=${roleIds.join(",")}`
    );
};


//根据用户名或姓名分页获取用户列表
export const getUserListApi = (
    params?: IUserListParams
): RSP<IUserDate<IUserList>> => request.get("/admin/list", { params });

export const queryRoleListApi = (
    params: IUserListParams
): RSP<IUserDate<IRoleModel>> => request.get("/role/list", { params });

// 分配角色列表的请求
export const getAllResourceApi = (): RSP<IResourceModel[]> =>
    request.get("/resource/listAll");
// 根据角色名称分页获取角色列表
export const getAllResourceCategoryApi = (): RSP<IResourceCategoryModel[]> =>
    request.get("/resourceCategory/listAll");

// 获取角色相关资源
export const getListRoleResourceApio = (
    roleId: number
): RSP<IResourceModel[]> => request.get(`/role/listResource/${roleId}`);


interface IAllocResourceParams {
    resourceIds: number[];
    roleId: number;
}
// 给角色分配资源
export const allocResourceApi = (params: IAllocResourceParams): RSP<any> => request.post("/role/allocResource?" + qs.stringify(params, { arrayFormat: "comma" }))


